<template>
	<view class="recharge">
		<view class="box">
			<view class="title">充值金额</view>
			<view class="inputBox">
				<text class="icon">¥</text>
				<u-input placeholder="请输入充值金额" v-model="price" border="none"></u-input>
			</view>
			<view class="info">温馨提示: 提示文本内容提示文本内容提示文本内容提示文本内容</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view class="list">
			<image src="../../../static/wx.png" mode="" class="icon"></image>
			<view class="info">微信支付</view>
			<view class="select">
				<u-icon v-if="type === 1" name="checkmark-circle-fill" color="#FFC141"></u-icon>
			</view>
		</view>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<view class="list" @click="type = 0">
			<image src="../../../static/ali.png" mode="" class="icon"></image>
			<view class="info">支付宝支付</view>
			<view class="select">
				<u-icon v-if="type === 0" name="checkmark-circle-fill" color="#FFC141"></u-icon>
			</view>
		</view>
		<!-- #endif -->
		<!-- #ifdef MP-TOUTIAO -->
		<view class="list" @click="type = 3">
			<image src="https://www.stck.cc/my/dou.png" mode="" class="icona"></image>
			<view class="info">抖音支付</view>
			<view class="select">
				<u-icon v-if="type === 3" name="checkmark-circle-fill" color="#FFC141"></u-icon>
			</view>
		</view>
		<!-- #endif -->

		<view class="btn" @click="recharge">立即充值</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 1,
				price: ''
			};
		},
		methods: {
			recharge() {
				let that = this
				if(!uni.$u.test.amount(this.price)) {
					this.$u.toast('请输入合法的金额');
					return
				}
				uni.$u.http.post('/recharge', {
					price: this.price,
					// #ifdef APP-PLUS
					paytype: this.type,
					// #endif
					// #ifdef MP-WEIXIN
					paytype: 2
					// #endif
					// #ifdef MP-TOUTIAO
					paytype: 3
					// #endif
				}).then(res => {
					if (res.code == 1) {
						
						// #ifdef MP-WEIXIN
						console.log(222)
						uni.requestPayment({
							appId: res.data.pay.appId,
							nonceStr: res.data.pay.nonceStr,
							package: res.data.pay.package,
							paySign: res.data.pay.paySign,
							signType: res.data.pay.signType,
							timeStamp: res.data.pay.timeStamp,
							success: (e) => {
								that.$u.toast('购买成功');
								setTimeout(() => {
									uni.$u.route({
										type: 'back'
									})
								}, 1000)
							},
							fail: (e) => {
								that.$u.toast('支付失败');
							}
						})
						// #endif
						// #ifdef APP-PLUS
						uni.requestPayment({
							provider: this.type == 1 ? 'wxpay' : 'alipay',
							orderInfo: res.data, //微信、支付宝订单数据
							success: function(res) {
								// console.log('success:' + JSON.stringify(res));
								// alert(JSON.stringify(res));
								that.$u.toast('支付成功');
								setTimeout(() => {
									uni.$u.route({
										type: 'back'
									})
								}, 1000)
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
								that.$u.toast('支付失败');
								// alert(JSON.stringify(err));
							}
						});
						// #endif
						// #ifdef MP-TOUTIAO
						console.log(333)
						uni.requestPayment({
							orderInfo: {
								order_id: res.data.pay.data.order_id,
								order_token: res.data.pay.data.order_token
							},
							service: 5,
							getOrderStatus: e => {
								console.log(e)
							},
							success: response => {
								if (response.code == 0) {
									console.log('支付成功')
								}
							},
							fail: (e) => {
								console.log(e)
								this.toast('充值失败');
							}
						})
						// #endif
						// uni.requestPayment({
						// 	provider: this.type == 1 ? 'wxpay' : 'alipay',
						// 	orderInfo: res.data, //微信、支付宝订单数据
						// 	success: function(res) {
						// 		// console.log('success:' + JSON.stringify(res));
						// 		// alert(JSON.stringify(res));
						// 		that.$u.toast('支付成功');
						// 		uni.$emit('money')
						// 	},
						// 	fail: function(err) {
						// 		console.log('fail:' + JSON.stringify(err));
						// 		that.$u.toast('支付失败');
						// 		// alert(JSON.stringify(err));
						// 	}
						// });
					}
				}).catch((res) => {

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.recharge {
		height: 100vh;
		background-color: #F8FAF9;
		box-sizing: border-box;
		padding-top: 20rpx;

		.box {
			margin: 0 20rpx 40rpx;
			background: #fff;
			border-radius: 10rpx;
			padding: 20rpx;

			.title {
				color: #333333;
				font-size: 28rpx;
			}

			.inputBox {
				display: flex;
				align-items: center;
				margin: 20rpx 0;
				border-bottom: 1px solid #F4F4F4;
				padding-bottom: 20rpx;

				.icon {
					color: #333;
					font-weight: bold;
					font-size: 48rpx;
					margin-right: 20rpx;
				}
			}

			.info {
				color: #666;
				font-size: 26rpx;
			}
		}

		.list {
			height: 130rpx;
			margin: 20rpx;
			border-radius: 10rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			padding: 0 20rpx;

			.icon {
				width: 86rpx;
				height: 86rpx;
				margin-right: 20rpx;
			}
			
			.icona{
				width: 43rpx;
				height: 53rpx;
				margin-right: 20rpx;
			}

			.info {
				flex: 1;
				font-size: 32rpx;
				color: #333;
			}

			.select {
				width: 32rpx;
				height: 32rpx;
				border: 1px solid #EEEEEE;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
			}
		}

		.btn {
			margin: 200rpx auto 0;
			width: 440rpx;
			height: 88rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 44rpx;
			background: linear-gradient(to right, #FFDF41, #FFC141);
		}
	}
</style>
